# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.classfyDao import ClassfyAPI
from com.zzh.dao.itemDao import ItemDao

from . import app

CORS(app, supports_credentials=True)

'''分类表 zzh_classfy API'''


@app.route('/query-classfy/<int:index>', methods=['GET'])
def queryClassfy(index):
    """
    添加一个参数，确定查询的是哪一级分类，传递的是分类的编号
    :param index: 页码索引
    :return:
    """
    queryResultOfDatabaseClassfy, classfyListJson = ClassfyAPI().queryClassfy(index)  # 返回值的获取顺序要匹配
    if queryResultOfDatabaseClassfy:
        return jsonify({"message": {"code": 1, "info": "已查询到分类信息"}, "totalCount": queryResultOfDatabaseClassfy,
                        "hotsearchListJson": classfyListJson})
    else:
        return jsonify({"message": {"code": 0, "info": "未查询到分类信息"}})


@app.route('/add-classfy', methods=['POST'])
def addClassfy():
    if not request.json:
        abort(400)
    addClassfy = {
        "classfyName": request.json['classfyName'],
        "classfyPid": request.json['classfyPid'],
        "classfyCode": request.json['classfyCode'],
        "classfyStatus": request.json.get('classfyStatus', 1),
        "classfySort": request.json.get('classfySort', 0)
    }
    result = ClassfyAPI().addClassfy(addClassfy)
    if result:
        return jsonify({"addClassfyInfo": addClassfy, "message": {"code": 1, "info": "添加成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "添加失败！"}})


@app.route('/update-classfy/<int:classfyId>', methods=['POST'])  # 根据classfyId更新zzh_classfy表指定记录
def updateClassfyById(classfyId):
    if not request.json:
        print request.json
        abort(400)
    updateClassfy = {
        "classfyName": request.json['classfyName'],
        "classfyPid": request.json['classfyPid'],
        "classfyCode": request.json['classfyCode'],
        "classfyStatus": request.json.get('classfyStatus', 1),
        "classfySort": request.json.get('classfySort', 0)
    }
    updateClassfyResult = ClassfyAPI().updateClassfy(updateClassfy, classfyId)
    if updateClassfyResult:
        return jsonify({"updateClassfyInfo": updateClassfy,
                        "massage": {"code": 1, "info": "updateClassfy OK"}})  # 添加函数调用成功，返回添加的信息（不包括hotsearch_id）
    else:
        return jsonify({"message": {"code": 0, "info": "updateClassfy Failed"}})  # 添加函数调用失败


@app.route('/delete-classfy/<int:classfyId>', methods=['DELETE'])  # 根据hotsearchId删除zzh_hotsearch表指定记录
def deleteClassfyById(classfyId):
    deleteClassfyResult = ClassfyAPI().deleteClassfyById(classfyId)
    if deleteClassfyResult:
        return jsonify({"message": {"code": 1, "info": "删除分类记录操作成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "删除分类记录操作失败！"}})


# -------------分类页面 API   2017-3-21    ----------------
@app.route('/get-item-by-classfy', methods=['POST'])
def get_items_by_classfy():
    if not request.json:
        abort(400)
    classfycode = request.json['classfycode']
    items = ItemDao().get_items_by_classfy(classfycode)

    return jsonify({"items": items, "message": {"code": 1, "info": "OK！"}})


@app.route('/query-classfy-name', methods=['GET'])
def queryClassfyName():
    queryClassfyNameResultJSON = ClassfyAPI().queryClassfyName()
    print queryClassfyNameResultJSON
    if queryClassfyNameResultJSON:
        return jsonify({"message": {"code": 1, "info": "OK"},
                        "classfyName": queryClassfyNameResultJSON})


# ---------------获取一级分类，二级分类，三级分类信息--------------------------
@app.route('/query-classfy-one', methods=['POST'])
def query_classfy_one():
    if not request.json:
        abort(400)
    classfy_code = request.json.get('classfy_code', '')

    classfys = ClassfyAPI().query_classfy_one(classfy_code)

    return jsonify({"classfyList": classfys, "message": {"code": 1, "info": "OK！"}})
